
package com.accp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import cn.hutool.http.HttpUtil;

public class MPGenerator {
	
	/*
	public static void main(String[] args) throws Exception {
		// 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        final String projectPath = System.getProperty("user.dir");//获取当前项目物理路径
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("辉");//设置作者名
        gc.setOpen(false);
        gc.setFileOverride(true);//生成的文件覆盖旧文件
        gc.setBaseResultMap(true);//生成BaseResultMap
        gc.setBaseColumnList(true);//生成BaseColumnList
        gc.setActiveRecord(true);//开启ActiveRecord模式
        gc.setIdType(IdType.AUTO);//设置主键生成策略
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/yami_shops?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //自定义模块名
        final String moduleName="accp";
        pc.setModuleName(moduleName);
        pc.setParent("com");//==== 包名（自己手动设置）
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList();
        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称================================模块名（自己设置）
                return projectPath + "/src/main/resources/mapper/"   
                         + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置**（个性化定制）**
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        
        //实体类自动继承Entity,不需要也可以
        //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //控制层自动继承父类BaseController,不需要也可以
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        //设置要被扫描的表名,可传多个参数
        
        List<Entity> tableName = Db.use().query("show tables;");
        for (int i = 0; i < tableName.size(); i++) {
        	 System.out.println(tableName.get(i));
        	 
		}
      
        List<Entity> entity = Db.use().query("show tables;");
        String[] enStr = new String[entity.size()];
        for (int i = 0; i < entity.size(); i++) {
        	enStr[i]=entity.get(i).getStr("Tables_in_yami_shops");        	 
		}
        
        strategy.setInclude(enStr);
        
        mpg.setStrategy(strategy);
        mpg.execute();
		
		
		
		
	}*/
	
}
